//
//  FDAlertView.m
//  fruitsDealers
//
//  Created by sgone-1 on 16/3/17.
//  Copyright © 2016年 com.sgone. All rights reserved.
//

#import "FDAlertView.h"
#import "FDBackgroundView.h"
#import "Common.h"
#import "UIView+Helper.h"
#import "labelAdaptive.h"
@interface FDAlertView () <UITextFieldDelegate> {
    
    
}

@property (nonatomic, strong)UIView *bigView;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *contentLabel;
@property (nonatomic, strong)UIButton *cancelButton;
@property (nonatomic, strong)UIButton *confirmButton;

@end

@implementation FDAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

//- (UIView *)setCustomAlertViewWithTitle:(NSString *)title message:(NSAttributedString *)message cancel:(NSString *)cancel verify:(NSString *)verify controller:(UIViewController *)controller {
//    
//    FDAlertView *view = [[FDAlertView alloc] initWithFrame:CGRectMake(0, 0, FDSCREEN_WIDTH, FDSCREEN_HEIGHT)];
//    [view setCustomAlertViewWithTitle:title message:message cancel:cancel verify:verify];
//    view.delegate = controller;
//    return view;
//}
//
//+ (UIView *)setCustomAlertViewWithTitle:(NSString *)title message:(NSAttributedString *)message cancel:(NSString *)cancel verify:(NSString *)verify controller:(UIViewController *)controller {
//    
//    return [self setCustomAlertViewWithTitle:title message:message cancel:cancel verify:verify controller:controller];
//}

- (void)setCustomAlertViewWithTitle:(NSString *)title message:(NSAttributedString *)message cancel:(NSString *)cancel verify:(NSString *)verify {
    
    FDBackgroundView *backView = [[FDBackgroundView alloc] initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
    [self addSubview:backView];
    
    self.bigView = [[UIView alloc] init];
    self.bigView.layer.cornerRadius = 5.0f;
    self.bigView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.bigView];
    
    CGFloat width = backView.width / 6 * 5;
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 35 / FDLaOt)];
    topView.layer.cornerRadius = 5.0f;
    topView.backgroundColor = [UIColor colorWithRed:238.0/255.0f green:72.0/255.0f blue:38.0/255.0f alpha:1];
    [self.bigView addSubview:topView];
    
    UIView *addView = [[UIView alloc] initWithFrame:CGRectMake(0, topView.height - 6, topView.width, 6)];
    addView.backgroundColor = [UIColor colorWithRed:238.0/255.0f green:72.0/255.0f blue:38.0/255.0f alpha:1];
    [self.bigView addSubview:addView];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:topView.frame];
    self.titleLabel.text = title;
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.font = [UIFont systemFontOfSize:15 / FDLaOt];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
//    self.titleLabel.backgroundColor = FDMainColorAla;
    [self.bigView addSubview:self.titleLabel];
    
    if (message.length != 0) {
        
        self.contentLabel = [[UILabel alloc] init];
        self.contentLabel.attributedText = message;
        //    self.contentLabel.textColor = FDFont_COLOR;
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
        self.contentLabel.textAlignment = NSTextAlignmentCenter;
        CGSize contentSize = [labelAdaptive sizeWithString:self.contentLabel.text font:self.contentLabel.font];
        self.contentLabel.frame = CGRectMake(0, self.titleLabel.height + 8, self.titleLabel.width, contentSize.height);
        [self.bigView addSubview:self.contentLabel];
    } else {
        
        self.passwordField = [[UITextField alloc] initWithFrame:CGRectMake(8, self.titleLabel.height + 16, width - 16, 35 / FDLaOt)];
        self.passwordField.borderStyle = UITextBorderStyleNone;
        self.passwordField.placeholder = @"输入提现密码";
        self.passwordField.backgroundColor = [UIColor whiteColor];
        self.passwordField.returnKeyType = UIReturnKeyDone;
        [self.passwordField setSecureTextEntry:YES];
        self.passwordField.delegate = self;
        self.passwordField.font = [UIFont systemFontOfSize:14 / FDLaOt];
        [self.bigView addSubview:self.passwordField];
    }
    
    self.cancelButton = [[UIButton alloc] init];
    if (verify.length != 0) {
        
        if (message.length != 0) {
            
            self.cancelButton.frame = CGRectMake(16, self.contentLabel.y + self.contentLabel.height + 8, (width - 32) / 2, 35 / FDLaOt);
        } else
            self.cancelButton.frame = CGRectMake(16, self.passwordField.y + self.passwordField.height + 8, (width - 32) / 2, 35 / FDLaOt);
    } else {
        
        if (message.length != 0) {
            
            self.cancelButton.frame = CGRectMake(16, self.contentLabel.y + self.contentLabel.height + 8, (width - 32), 35 / FDLaOt);
        } else
            
            self.cancelButton.frame = CGRectMake(16, self.passwordField.y + self.passwordField.height + 8, (width - 32), 35 / FDLaOt);
    }
    
    
    [self.cancelButton setTitle:cancel forState:UIControlStateNormal];
    self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:14.0f / FDLaOt];
    [self.cancelButton setTitleColor:[UIColor colorWithRed:238.0/255.0f green:72.0/255.0f blue:38.0/255.0f alpha:1] forState:UIControlStateNormal];
    self.cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.cancelButton addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bigView addSubview:self.cancelButton];
    
    if (verify.length != 0) {
        
        self.confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(width / 2, self.cancelButton.y, self.cancelButton.width, self.cancelButton.height)];
        [self.confirmButton setTitle:verify forState:UIControlStateNormal];
        self.confirmButton.titleLabel.font = [UIFont systemFontOfSize:14.0f / FDLaOt];
        [self.confirmButton setTitleColor:[UIColor colorWithRed:238.0/255.0f green:72.0/255.0f blue:38.0/255.0f alpha:1] forState:UIControlStateNormal];
        self.confirmButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self.confirmButton addTarget:self action:@selector(verifyClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bigView addSubview:self.confirmButton];
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(self.confirmButton.x, self.cancelButton.y + 3, 0.5, (35 - 6) / FDLaOt)];
        label2.backgroundColor = [UIColor colorWithRed:223.0/255.0f green:223.0/255.0f blue:223.0/255.0f alpha:1];
        
        [self.bigView addSubview:label2];
    }
    
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(16, self.cancelButton.y - 0.5, width - 32, 0.5)];
    label1.backgroundColor = [UIColor colorWithRed:223.0/255.0f green:223.0/255.0f blue:223.0/255.0f alpha:1];
;
    
    [self.bigView addSubview:label1];
    
    CGFloat height = self.cancelButton.y + self.cancelButton.height;
    self.bigView.frame = CGRectMake(backView.width / 6 / 2, (backView.height - height) / 2, backView.width / 6 * 5, height);
}

+ (void)setCustomAlertViewWithTitle:(NSString *)title message:(NSAttributedString *)message cancel:(NSString *)cancel verify:(NSString *)verify {
    
    return [self setCustomAlertViewWithTitle:title message:message cancel:cancel verify:verify];
}

- (void)setAlertViewWithTitle:(NSString *)title message:(NSString *)message cancel:(NSString *)cancel verify:(NSString *)verify {
    
    FDBackgroundView *backView = [[FDBackgroundView alloc] initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
    [self addSubview:backView];
    
    self.bigView = [[UIView alloc] init];
    self.bigView.layer.cornerRadius = 5.0f;
    self.bigView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.bigView];
    
    CGFloat width = backView.width / 6 * 4;
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 30 / FDLaOt)];
    topView.layer.cornerRadius = 5.0f;
    topView.backgroundColor = [UIColor colorWithRed:238.0/255.0f green:72.0/255.0f blue:38.0/255.0f alpha:1];
    [self.bigView addSubview:topView];
    
    UIView *addView = [[UIView alloc] initWithFrame:CGRectMake(0, topView.height - 6, topView.width, 6)];
    addView.backgroundColor = [UIColor colorWithRed:238.0/255.0f green:72.0/255.0f blue:38.0/255.0f alpha:1];
    [self.bigView addSubview:addView];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:topView.frame];
    self.titleLabel.text = title;
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.font = [UIFont systemFontOfSize:15 / FDLaOt];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    //    self.titleLabel.backgroundColor = FDMainColorAla;
    [self.bigView addSubview:self.titleLabel];
    
    if (message.length != 0) {
        
        self.contentLabel = [[UILabel alloc] init];
        self.contentLabel.text = message;
        //    self.contentLabel.textColor = FDFont_COLOR;
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
        self.contentLabel.textAlignment = NSTextAlignmentCenter;
        CGSize contentSize = [labelAdaptive sizeWithString:self.contentLabel.text font:self.contentLabel.font];
        self.contentLabel.frame = CGRectMake(0, self.titleLabel.height + 8, self.titleLabel.width, contentSize.height);
        [self.bigView addSubview:self.contentLabel];
    } else {
        
        self.passwordField = [[UITextField alloc] initWithFrame:CGRectMake(8, self.titleLabel.height + 16, width - 16, 35 / FDLaOt)];
        self.passwordField.borderStyle = UITextBorderStyleNone;
        self.passwordField.placeholder = @"输入提现密码";
        self.passwordField.backgroundColor = [UIColor whiteColor];
        self.passwordField.returnKeyType = UIReturnKeyDone;
        [self.passwordField setSecureTextEntry:YES];
        self.passwordField.delegate = self;
        self.passwordField.font = [UIFont systemFontOfSize:14.0f / FDLaOt];
        [self.bigView addSubview:self.passwordField];
    }
    
    self.cancelButton = [[UIButton alloc] init];
    if (verify.length != 0) {
        
        if (message.length != 0) {
            
            self.cancelButton.frame = CGRectMake(16, self.contentLabel.y + self.contentLabel.height + 8, (width - 32) / 2, 35 / FDLaOt);
        } else
            self.cancelButton.frame = CGRectMake(16, self.passwordField.y + self.passwordField.height + 8, (width - 32) / 2, 35 / FDLaOt);
    } else {
        
        if (message.length != 0) {
            
            self.cancelButton.frame = CGRectMake(16, self.contentLabel.y + self.contentLabel.height + 8, (width - 32), 35 / FDLaOt);
        } else
            
            self.cancelButton.frame = CGRectMake(16, self.passwordField.y + self.passwordField.height + 8, (width - 32), 35 / FDLaOt);
    }
    
    
    [self.cancelButton setTitle:cancel forState:UIControlStateNormal];
    self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:14.0f / FDLaOt];
    [self.cancelButton setTitleColor:[UIColor colorWithRed:238.0/255.0f green:72.0/255.0f blue:38.0/255.0f alpha:1] forState:UIControlStateNormal];
    self.cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.cancelButton addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bigView addSubview:self.cancelButton];
    
    if (verify.length != 0) {
        
        self.confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(width / 2, self.cancelButton.y, self.cancelButton.width, self.cancelButton.height)];
        [self.confirmButton setTitle:verify forState:UIControlStateNormal];
        self.confirmButton.titleLabel.font = [UIFont systemFontOfSize:14.0f / FDLaOt];
        [self.confirmButton setTitleColor:[UIColor colorWithRed:238.0/255.0f green:72.0/255.0f blue:38.0/255.0f alpha:1] forState:UIControlStateNormal];
        self.confirmButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self.confirmButton addTarget:self action:@selector(verifyClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bigView addSubview:self.confirmButton];
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(self.confirmButton.x, self.cancelButton.y + 3, 0.5, (35 - 6) / FDLaOt)];
        label2.backgroundColor = [UIColor colorWithRed:238.0/255.0f green:72.0/255.0f blue:38.0/255.0f alpha:1];
        
        [self.bigView addSubview:label2];
    }
    
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(16, self.cancelButton.y - 0.5, width - 32, 0.5)];
    label1.backgroundColor = [UIColor colorWithRed:238.0/255.0f green:72.0/255.0f blue:38.0/255.0f alpha:1];
    
    [self.bigView addSubview:label1];
    
    CGFloat height = self.cancelButton.y + self.cancelButton.height;
    self.bigView.frame = CGRectMake(backView.width / 6, (backView.height - height) / 2, backView.width / 6 * 4, height);
}

+ (void)setAlertViewWithTitle:(NSString *)title message:(NSString *)message cancel:(NSString *)cancel verify:(NSString *)verify {
    
    return [self setAlertViewWithTitle:title message:message cancel:cancel verify:verify];
}

- (void)closeClick {
    
    [self removeFromSuperview];
}

- (void)verifyClick {
    
    [self removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(alertViewConfirmClick)]) {
        
        [self.delegate alertViewConfirmClick];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    return YES;
}

@end
